package cn.tedu.lbs.basic.web.controller;

import cn.tedu.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import com.tarena.lbs.pojo.basic.vo.BusinessCategoryVO;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理商家相关所有功能
 */
@Slf4j
@RestController
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @GetMapping("/admin/basic/business/info/list")
    public Result<PageResult<BusinessVO>> pageList(BusinessQuery query){
        log.info("商家后台分页，入参：{}",query);
        PageResult<BusinessVO> voPage = businessService.pageList(query);
        log.info("商家后台分页，入参：{};出参：{}",query,voPage);
        return new Result<>(voPage);
    }
    //查询商家行业分类列表
    @GetMapping("/admin/basic/business/category/list")
    public Result<List<BusinessCategoryVO>> categoryList(){
        return new Result<>(businessService.categoryList());
    }
    //新增提交商家表单

    /**
     * springmvc 框架中默认 转化json的转化器 无法将时间字符串转化成日期类型
     * startTime="2024-01-01 12:0:0"
     * {
     *     "startTime":"2024-01-01 12:0:0"
     * }
     */
    @PostMapping("/admin/basic/business/info/save")
    public Result<Void> save(@RequestBody BusinessParam param) throws BusinessException{
        //save方法是幂等的，同一个商家名称不允许重复提交
        for(int i=0;i<3;i++){
            log.info("第{}次尝试提交同一个商家信息",i+1);
            businessService.save(param);
        }
        return Result.success();
    }
}
